package com.huixin.zhang.common.bean.code;

import com.github.pagehelper.util.StringUtil;
import com.huixin.zhang.common.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Data;


@Data
@AllArgsConstructor
public class ResultBean {
	private int code;
	private String msg;
	private Object data;

	public boolean isSuccess(){
		return code == ResultCode.SUCCESS.getCode();
	}

	public boolean isFailure(){
		return code != ResultCode.SUCCESS.getCode();
	}
	
	public static ResultBean success() {
		ResultCode resultCode = ResultCode.SUCCESS;
		ResultBean result = new ResultBean(resultCode.getCode(), resultCode.getMsg(), null);
		return result;
	}
	
	public static ResultBean success(Object data) {
		ResultCode resultCode = ResultCode.SUCCESS;
		ResultBean result = new ResultBean(resultCode.getCode(), resultCode.getMsg(), data);
		return result;
	}

	/**
	 * 登录超时
	 * @return
	 */
	public static ResultBean timeout(){
		return failure(ResultCode.ERROR_100);
	}

	/**
	 * 数据不存在
	 * @return
	 */
	public static ResultBean notfund(){
		return failure(ResultCode.ERROR_103);
	}
	
	public static ResultBean failure(int code, String msg) {
		ResultBean result = new ResultBean(code, msg, null);
		return result;
	}
	
	public static ResultBean failure(ResultCode resultCode) {
		ResultBean result = new ResultBean(resultCode.getCode(), resultCode.getMsg(), null);
		return result;
	}
	
	public static ResultBean failure(ResultCode resultCode, String msg) {
		if(StringUtil.isEmpty(msg)) {
			msg = resultCode.getMsg();
		}
		ResultBean result = new ResultBean(resultCode.getCode(), msg, null);
		return result;
	}
}
